#include "wap_roller.h"
#include "wa_wm.h"
#include "wa_win.h"
#include "wa_screen.h"
#include "wa_roller.h"
#include "wa_label.h"
#include "wap.h"
#include "stdio.h"

static void wap_roller_create(void);
static void wap_roller_del(void);
static void wap_roller_callback(wa_sign* sign);

wa_screen_obj wap_roller_screen = 
{
	.id = WAP_WIN_ROLLER_ID,
	.create = wap_roller_create,
	.del = wap_roller_del,
};

wa_obj* wap_roller_win;
wa_obj* wap_roller_roller_h;
wa_obj* wap_roller_roller_v;
wa_obj* wap_roller_label_h;
wa_obj* wap_roller_label_v;

extern wa_font font_song_16_ascii;

uint8_t wap_roller_h_select;
uint8_t wap_roller_v_select;

void wap_roller_init(void)
{
	wap_roller_h_select = 0;
	wap_roller_v_select = 0;
}

static void wap_roller_create(void)
{
	wap_roller_win = wa_win_create(NULL);
	wa_win_set_pos(wap_roller_win, 0, 0);
	wa_win_set_size(wap_roller_win, WA_LCD_W, WA_LCD_H);
	wa_win_set_edge_show(wap_roller_win, wa_nok);
	wa_win_set_callback(wap_roller_win, wap_roller_callback);
	wap_roller_screen.win = wap_roller_win;
	
	wap_roller_label_h = wa_label_create(NULL);
	wa_win_add_obj(wap_roller_win, wap_roller_label_h);
	wa_obj_set_pos(wap_roller_label_h, 8, 12);
	wa_obj_set_size(wap_roller_label_h, 32, 16);
	wa_label_set_font(wap_roller_label_h, &font_song_16_ascii);
	wa_label_set_text(wap_roller_label_h, "hor:");
	
	wap_roller_label_v = wa_label_create(NULL);
	wa_win_add_obj(wap_roller_win, wap_roller_label_v);
	wa_obj_set_pos(wap_roller_label_v, 8, 36);
	wa_obj_set_size(wap_roller_label_v, 32, 16);
	wa_label_set_font(wap_roller_label_v, &font_song_16_ascii);
	wa_label_set_text(wap_roller_label_v, "ver:");

	wap_roller_roller_h = wa_roller_create(NULL);
	wa_win_add_obj(wap_roller_win, wap_roller_roller_h);
	wa_obj_set_pos(wap_roller_roller_h, 48, 12);
	wa_obj_set_size(wap_roller_roller_h, 64, 16);
	wa_roller_set_type(wap_roller_roller_h, WA_ROLLER_TYPE_HOR);
	wa_roller_add_option(wap_roller_roller_h, "left");
	wa_roller_add_option(wap_roller_roller_h, "center");
	wa_roller_add_option(wap_roller_roller_h, "right");
	wa_roller_set_font(wap_roller_roller_h, &font_song_16_ascii);
	wa_roller_set_space(wap_roller_roller_h, 16);
	wa_roller_set_switch_time_length(wap_roller_roller_h, 200);
	wa_roller_set_select(wap_roller_roller_h, wap_roller_h_select);
	wa_roller_init_select_pos(wap_roller_roller_h);
	wa_roller_set_anim_enable(wap_roller_roller_h, wa_ok);
	
	wap_roller_roller_v = wa_roller_create(NULL);
	wa_win_add_obj(wap_roller_win, wap_roller_roller_v);
	wa_obj_set_pos(wap_roller_roller_v, 48, 36);
	wa_obj_set_size(wap_roller_roller_v, 64, 16);
	wa_roller_set_type(wap_roller_roller_v, WA_ROLLER_TYPE_VER);
	wa_roller_add_option(wap_roller_roller_v, "up");
	wa_roller_add_option(wap_roller_roller_v, "center");
	wa_roller_add_option(wap_roller_roller_v, "down");
	wa_roller_set_font(wap_roller_roller_v, &font_song_16_ascii);
	wa_roller_set_space(wap_roller_roller_v, 4);
	wa_roller_set_switch_time_length(wap_roller_roller_v, 200);
	wa_roller_set_select(wap_roller_roller_v, wap_roller_v_select);
	wa_roller_init_select_pos(wap_roller_roller_v);
	wa_roller_set_anim_enable(wap_roller_roller_v, wa_ok);
}

static void wap_roller_del(void)
{
	wa_obj_del(wap_roller_win);
}

static void _wap_roller_paint(wa_obj* obj)
{
}

static void wap_roller_callback(wa_sign* sign)
{
	int sign_id;
	wa_obj* obj;
	sign_id = sign->sign_id;
	obj = sign->obj_dst;

	switch (sign_id)
	{
		case WA_SIGNAL_PAINT:
		{
			_wap_roller_paint(obj);
			break;
		}
		case WA_SIGNAL_NOTIFY_PARENT:
		{	
			break;
		}
		case WA_SIGNAL_USER_KEY_UP:
		{	
			if(wap_roller_v_select > 0)
			{
				wap_roller_v_select--;
			}
			wa_roller_set_select(wap_roller_roller_v, wap_roller_v_select);
			break;
		}
		case WA_SIGNAL_USER_KEY_DOWN:
		{	
			if(wap_roller_v_select < 2)
			{
				wap_roller_v_select++;
			}
			wa_roller_set_select(wap_roller_roller_v, wap_roller_v_select);
			break;
		}
		case WA_SIGNAL_USER_KEY_LEFT:
		{	
			if(wap_roller_h_select < 2)
			{
				wap_roller_h_select++;
			}
			wa_roller_set_select(wap_roller_roller_h, wap_roller_h_select);
			break;
		}
		case WA_SIGNAL_USER_KEY_RIGHT:
		{	
			if(wap_roller_h_select > 0)
			{
				wap_roller_h_select--;
			}
			wa_roller_set_select(wap_roller_roller_h, wap_roller_h_select);
			break;
		}
		case WA_SIGNAL_USER_KEY_ESC:
		{	
			wa_screen_switch(WAP_WIN_MENU_ID, WA_SCREEN_SWITCH_TYPE_LEFT_TOGETHER, 500, wa_nok, wa_nok);
			break;
		}
		default:
		{
			break;
		}
	}
}


